//
//  KSAccountSettingViewController.swift
//  Supermarket
//
//  Created by Black_Crystal on 2017/9/20.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

class KSAccountSettingViewController: KSBaseViewController {

    @IBOutlet weak var tableview: UITableView!
    private let dataArray = ["修改密码","帮助与反馈","清除缓存","关于吉拍档"]
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "账户设置"
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.backgroundColor = KSColor.kColorViewBackground
        self.tableview.register(KSOnlyTitleTableViewCell.self, forCellReuseIdentifier: "KSOnlyTitleTableViewCell")
        self.tableview.register(KSAccountSettingTableViewCell.self, forCellReuseIdentifier: "KSAccountSettingTableViewCell")
        self.tableview.register(KSQuitLoginTableViewCell.self, forCellReuseIdentifier: "KSQuitLoginTableViewCell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

extension KSAccountSettingViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        var cellHeight: CGFloat = 50
        if  indexPath.row == 0 {
            cellHeight = 166
        } else if indexPath.row == 5{
            cellHeight = 60
        }
        return cellHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableview.dequeueReusableCell(withIdentifier: "KSAccountSettingTableViewCell", for: indexPath) as! KSAccountSettingTableViewCell
            cell.setbackImage(image: "Account_BG")
            cell.selectionStyle = .none
            cell.headImage.sd_setImage(with: nil, placeholderImage: UIImage.init(named: "Account_accountPlace.png"))
            return cell
        } else if indexPath.row == 5 {
            let cell = tableview.dequeueReusableCell(withIdentifier: "KSQuitLoginTableViewCell", for: indexPath) as! KSQuitLoginTableViewCell
            cell.selectionStyle = .none
            return cell
        } else  {
            let cell = tableview.dequeueReusableCell(withIdentifier: "KSOnlyTitleTableViewCell", for: indexPath) as! KSOnlyTitleTableViewCell
            cell.selectionStyle = .none
            if !(indexPath.row == 3) {
                cell.accessoryType = .disclosureIndicator
            } else {
                cell.cacheLabel.text = KSTools.getCache()
            }
            cell.selectionStyle = .none
            cell.titleLabel.text = dataArray[indexPath.row - 1]
            if indexPath.row == 4 {
                cell.lineView.isHidden = true
            }
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 1 {
            let vc = KSChangePasswordViewController.init(nibName: "KSChangePasswordViewController", bundle: nil)
            self.navigationController?.pushViewController(vc, animated: true)
        } else if indexPath.row == 2 {
            self.navigationController?.pushViewController(KSFeedbackViewController(), animated: true)
        } else if indexPath.row == 3 {
            let alertViewController = UIAlertController.init(title: nil, message: "是否清除缓存", preferredStyle: .alert)
            let confirmAlert = UIAlertAction.init(title: "确定", style: .default) { (UIAlertAction) in
                KSTools.clearCache()
                self.progressHUD.showMessage(text: "缓存清除成功", duration: 2, animated: true)
                tableView.reloadData()
            }
            let cancelAlert = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
            alertViewController.addAction(confirmAlert)
            alertViewController.addAction(cancelAlert)
            self.present(alertViewController, animated: true) {}
        } else if indexPath.row == 4 {
            self.navigationController?.pushViewController(KSAboutViewController(), animated: true)
        } else if indexPath.row == 5 {
            let alertViewController = UIAlertController.init(title: "温馨提示", message: "您确定退出吗", preferredStyle: .actionSheet)
            let confirmAlert = UIAlertAction.init(title: "确定", style: .default) { (UIAlertAction) in
                KSUserDefaultsTools.setUserHaveLogined(false)
                let appDelegate =  UIApplication.shared.delegate as! AppDelegate
                appDelegate.switchLoginPage()
            }
            let cancelAlert = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
            alertViewController.addAction(confirmAlert)
            alertViewController.addAction(cancelAlert)
            self.present(alertViewController, animated: true) {}
           
        }
    }
}


